<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/10/13
 * Time: 22:07
 */
namespace app\model;


use think\facade\Db;
use think\Model;

class UserReport extends Model
{

    const WEEK_REPORT = 1;
    const MONTH_REPORT = 2;

    private function _getListField()
    {
        return [
            'ur_id' => 'reportId',
            'ur_title' => 'title',
            'ur_content' => 'content',
            'ur_create_time' => 'createTime',
            'ur_type' => 'type',
            'ur_uid' => 'uid',
            'pername' => 'pername',
        ];
    }

    public function getReportInfoById($reportId,$field = '')
    {
        $field = $field ?: self::_getListField();
        return Db::table('user_report')
            ->leftJoin('user','user.id=ur_uid')
            ->leftJoin('organization','organization.id=ur_orgid')
            ->where(['ur_id'=>$reportId])->field($field)->find();
    }

    public function getReportInfoByWhere($where,$field = '')
    {
        $field = $field ?: self::_getListField();
        return Db::table('user_report')
            ->leftJoin('user','user.id=ur_uid')
            ->leftJoin('organization','organization.id=ur_orgid')
            ->where($where)->field($field)->find();
    }

    public function addOneReport($data)
    {
        $data['ur_create_time'] = time();
        return Db::table('user_report')->insertGetId($data);
    }

    public function getReportListByCondition($where = [],$order = 'ur_id desc',$field = '')
    {
        $field = $field ?: self::_getListField();
        $list = Db::table('user_report')
            ->leftJoin('user','user.id=ur_uid')
            ->leftJoin('organization','organization.id=ur_orgid')
            ->where($where)
            ->field($field)
            ->order($order)
            ->paginate(input('param.pageSize'))
            ->each(function ($item,$key){
                $item['content'] = json_decode($item['content'],1);
                return $item;
            })
            ->render();
        return $list;
    }

}